<script setup>
import { nextTick, onMounted, ref, watch } from 'vue'
import { useNewsStore, useOnlineUsersStore, useNameStore } from '@/stores'
import { userGetHistoryNewsService } from '@/api/news'
//import ws from '@/utils/websocket.js'
import { sendNewsService } from '@/api/news'
import { useUserStore } from '@/stores'
import { UserFilled } from '@element-plus/icons-vue'
const news = ref('')
const userStore = useUserStore()
const NewsStore = useNewsStore()
const nameStore = useNameStore()
const Main = ref()
const rol = ref()

const initHistoryNews = async () => {
  const history = await userGetHistoryNewsService()
  console.log('历史记录是')
  console.log(history)
  console.log(history.data)
  console.log(history.data.data)
  NewsStore.initNewsList(history.data.data)
  rolling()
}
initHistoryNews()
// 在线用户
const onlineUsersStore = useOnlineUsersStore()

// 向服务器发送消息
const sendNews = async () => {
  console.log('要发送的东西是:' + news.value)
  if (news.value.trim()) {
    const res = await sendNewsService(news.value)
    //ws.send(news.value)
    console.log('send返回的报文是：' + res)
    
  }
  news.value = ''
  setTimeout(() => {
    rolling()
  }, 1000);
}
// watch(
//   rol,
//   () => {
//     rolling()
//   },
//   { deep: true }
// )
const scrollbarRef = ref(null)
const rolling = () => {
  nextTick(() => {
    // 获取滚动条DOM元素
    const scrollbar = scrollbarRef.value.$el
    // 获取滚动条内部的滚动容器
    const container = scrollbar.querySelector('.el-scrollbar__view')
    // 计算需要滚动的距离
    const scrollHeight = container.scrollHeight
    const height = container.clientHeight
    // 滚动到底部
    scrollbarRef.value.scrollTo(0, scrollHeight)
    console.log('scrollBottom:' + scrollHeight)
  })
}
const dialogVisible = ref(false)
const viewUsers = ()=>{
  dialogVisible.value = true
}
</script>

<template>
  <div class="common-layout">
    <el-container>
      <!-- <el-aside width="200px">
        目前在线用户：
        <div v-for="item in onlineUsersStore.onlineUsersList" :key="item.id">
          {{ item.name }}({{ item.username }})
        </div>
      </el-aside> -->
      <el-tooltip effect="dark" :content="`当前在线人数${onlineUsersStore.onlineUsersList.length}人`" placement="top-start">
        <h1 class="title">
          群聊
          <span class="title-idx" @click="viewUsers">({{ onlineUsersStore.onlineUsersList.length }})</span>
        </h1>
      </el-tooltip>
      <div class="container">
        <el-scrollbar ref="scrollbarRef" class="scroll">
          <div v-for="item in NewsStore.newsList" :key="item.fromUser"
            :class="{ 'user-talk': item.fromUser == userStore.userName,'talk':true }" ref="rol">
            <div class="avatar">
              {{ nameStore.nameList[item.fromUser] }}
            </div>
            <div>
              <div class="talk-name">
                {{ nameStore.nameList[item.fromUser] }}({{ item.fromUser }})
              </div>
              <div class="talk-content">{{ item.content }}</div>
            </div>
          </div>
        </el-scrollbar>
        <!-- <div ref="roll" style="padding: 50px 0px"> -->
      </div>
      <!-- </div> -->
      <el-footer class="bottom">
        <el-input @keydown.enter.ctrl="sendNews" v-model="news" placeholder="ctrl+回车快速发送">
        </el-input>
        <el-button @click="sendNews">发送</el-button>
      </el-footer>
      <el-dialog v-model="dialogVisible">
        <template #title> 在线用户</template>
        <el-scrollbar max-height="60vh">
          <div v-for="item in onlineUsersStore.onlineUsersList" :key="item.id">
            {{ item.name }}({{ item.username }})
          </div>

        </el-scrollbar>
      </el-dialog>
    </el-container>
  </div>
</template>

<style lang="scss" scoped>
.class {
  height: 2000;
}

.textGreen {
  color: green;
}
.container{
  background-color: rgb(239, 239, 239);
  height: calc(80vh - 50px); ;

  padding: 10px;
}
.title{
  font-size: 18px;
  color: #409eff;
  margin-bottom: 20px;
  width: fit-content;
  &-idx{
    font-size: 14px;
    color: #909399;
    margin-left: 10px;
    cursor: pointer;
  }
}
.user-talk{
  margin-left: auto;
  flex-direction: row-reverse;
  .talk-name{
    text-align: right;
  }
}
.talk{
  display: flex;
  align-items: start;
  &-name{
    font-size: 13px;
    color: #959595;
    margin-top: 10px;
  }
  &-content{
    font-size: 14px;
    color: #303133;
    margin-top: 5px;
    background-color: #fff;
    border-radius: 15px;
    max-width: 50vw;
    padding: 5px;
    word-wrap: break-word;
    word-break: break-all;
  }
}
.avatar{
  width: 30px;
  flex-shrink: 0;
  height: 30px;
  font-size: 16px;
  background-color: #00318b;
  color: #cccccc;
  border-radius: 50%;
  text-align: center;
  line-height: 28px;
  margin-top: 10px;
  margin-left: 10px;
  overflow: hidden;
}
.bottom{
  display: flex;
  align-items: center;
  position: fixed;
  bottom: 10px;
  justify-content: center;
  width: calc(100% - 300px);
  padding: 0 40px;
  gap: 20px;
}
</style>
